/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package xcore.model.provider;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;

import org.eclipse.emf.edit.provider.ChangeNotifier;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IChangeNotifier;
import org.eclipse.emf.edit.provider.IDisposable;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.INotifyChangedListener;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;

import xcore.model.util.ModelAdapterFactory;

/**
 * This is the factory that is used to provide the interfaces needed to support Viewers.
 * The adapters generated by this factory convert EMF adapter notifications into calls to {@link #fireNotifyChanged fireNotifyChanged}.
 * The adapters also support Eclipse property sheets.
 * Note that most of the adapters are shared among multiple instances.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public class ModelItemProviderAdapterFactory extends ModelAdapterFactory implements ComposeableAdapterFactory, IChangeNotifier, IDisposable {
	/**
	 * This keeps track of the root adapter factory that delegates to this adapter factory.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ComposedAdapterFactory parentAdapterFactory;

	/**
	 * This is used to implement {@link org.eclipse.emf.edit.provider.IChangeNotifier}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IChangeNotifier changeNotifier = new ChangeNotifier();

	/**
	 * This keeps track of all the supported types checked by {@link #isFactoryForType isFactoryForType}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected Collection<Object> supportedTypes = new ArrayList<Object>();

	/**
	 * This constructs an instance.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ModelItemProviderAdapterFactory() {
		supportedTypes.add(IEditingDomainItemProvider.class);
		supportedTypes.add(IStructuredItemContentProvider.class);
		supportedTypes.add(ITreeItemContentProvider.class);
		supportedTypes.add(IItemLabelProvider.class);
		supportedTypes.add(IItemPropertySource.class);
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XClass} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XClassItemProvider xClassItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XClass}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXClassAdapter() {
		if (xClassItemProvider == null) {
			xClassItemProvider = new XClassItemProvider(this);
		}

		return xClassItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XReference} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XReferenceItemProvider xReferenceItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XReference}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXReferenceAdapter() {
		if (xReferenceItemProvider == null) {
			xReferenceItemProvider = new XReferenceItemProvider(this);
		}

		return xReferenceItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.Model} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ModelItemProvider modelItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.Model}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createModelAdapter() {
		if (modelItemProvider == null) {
			modelItemProvider = new ModelItemProvider(this);
		}

		return modelItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XObject} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XObjectItemProvider xObjectItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XObject}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXObjectAdapter() {
		if (xObjectItemProvider == null) {
			xObjectItemProvider = new XObjectItemProvider(this);
		}

		return xObjectItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XRelation} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XRelationItemProvider xRelationItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XRelation}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXRelationAdapter() {
		if (xRelationItemProvider == null) {
			xRelationItemProvider = new XRelationItemProvider(this);
		}

		return xRelationItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.Instance} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected InstanceItemProvider instanceItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.Instance}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createInstanceAdapter() {
		if (instanceItemProvider == null) {
			instanceItemProvider = new InstanceItemProvider(this);
		}

		return instanceItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XRefFeature} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XRefFeatureItemProvider xRefFeatureItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XRefFeature}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXRefFeatureAdapter() {
		if (xRefFeatureItemProvider == null) {
			xRefFeatureItemProvider = new XRefFeatureItemProvider(this);
		}

		return xRefFeatureItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XAttFeature} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XAttFeatureItemProvider xAttFeatureItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XAttFeature}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXAttFeatureAdapter() {
		if (xAttFeatureItemProvider == null) {
			xAttFeatureItemProvider = new XAttFeatureItemProvider(this);
		}

		return xAttFeatureItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XVarValues} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XVarValuesItemProvider xVarValuesItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XVarValues}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXVarValuesAdapter() {
		if (xVarValuesItemProvider == null) {
			xVarValuesItemProvider = new XVarValuesItemProvider(this);
		}

		return xVarValuesItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XVarAttribute} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XVarAttributeItemProvider xVarAttributeItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XVarAttribute}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXVarAttributeAdapter() {
		if (xVarAttributeItemProvider == null) {
			xVarAttributeItemProvider = new XVarAttributeItemProvider(this);
		}

		return xVarAttributeItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XCstAttribute} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XCstAttributeItemProvider xCstAttributeItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XCstAttribute}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXCstAttributeAdapter() {
		if (xCstAttributeItemProvider == null) {
			xCstAttributeItemProvider = new XCstAttributeItemProvider(this);
		}

		return xCstAttributeItemProvider;
	}

	/**
	 * This keeps track of the one adapter used for all {@link xcore.model.XCstValues} instances.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XCstValuesItemProvider xCstValuesItemProvider;

	/**
	 * This creates an adapter for a {@link xcore.model.XCstValues}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter createXCstValuesAdapter() {
		if (xCstValuesItemProvider == null) {
			xCstValuesItemProvider = new XCstValuesItemProvider(this);
		}

		return xCstValuesItemProvider;
	}

	/**
	 * This returns the root adapter factory that contains this factory.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ComposeableAdapterFactory getRootAdapterFactory() {
		return parentAdapterFactory == null ? this : parentAdapterFactory.getRootAdapterFactory();
	}

	/**
	 * This sets the composed adapter factory that contains this factory.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setParentAdapterFactory(ComposedAdapterFactory parentAdapterFactory) {
		this.parentAdapterFactory = parentAdapterFactory;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean isFactoryForType(Object type) {
		return supportedTypes.contains(type) || super.isFactoryForType(type);
	}

	/**
	 * This implementation substitutes the factory itself as the key for the adapter.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Adapter adapt(Notifier notifier, Object type) {
		return super.adapt(notifier, this);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object adapt(Object object, Object type) {
		if (isFactoryForType(type)) {
			Object adapter = super.adapt(object, type);
			if (!(type instanceof Class) || (((Class<?>)type).isInstance(adapter))) {
				return adapter;
			}
		}

		return null;
	}

	/**
	 * This adds a listener.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void addListener(INotifyChangedListener notifyChangedListener) {
		changeNotifier.addListener(notifyChangedListener);
	}

	/**
	 * This removes a listener.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void removeListener(INotifyChangedListener notifyChangedListener) {
		changeNotifier.removeListener(notifyChangedListener);
	}

	/**
	 * This delegates to {@link #changeNotifier} and to {@link #parentAdapterFactory}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void fireNotifyChanged(Notification notification) {
		changeNotifier.fireNotifyChanged(notification);

		if (parentAdapterFactory != null) {
			parentAdapterFactory.fireNotifyChanged(notification);
		}
	}

	/**
	 * This disposes all of the item providers created by this factory. 
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void dispose() {
		if (xClassItemProvider != null) xClassItemProvider.dispose();
		if (xReferenceItemProvider != null) xReferenceItemProvider.dispose();
		if (modelItemProvider != null) modelItemProvider.dispose();
		if (xObjectItemProvider != null) xObjectItemProvider.dispose();
		if (xRelationItemProvider != null) xRelationItemProvider.dispose();
		if (instanceItemProvider != null) instanceItemProvider.dispose();
		if (xRefFeatureItemProvider != null) xRefFeatureItemProvider.dispose();
		if (xAttFeatureItemProvider != null) xAttFeatureItemProvider.dispose();
		if (xVarValuesItemProvider != null) xVarValuesItemProvider.dispose();
		if (xVarAttributeItemProvider != null) xVarAttributeItemProvider.dispose();
		if (xCstAttributeItemProvider != null) xCstAttributeItemProvider.dispose();
		if (xCstValuesItemProvider != null) xCstValuesItemProvider.dispose();
	}

}
